import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Window extends JFrame implements MouseListener,KeyListener{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	JButton b1;
	Label l1, l2, l3;
	Map panel;
	int diff;
	int score = 0;
	String diffString;
	static int count = 1;
	Time time;
	Score sc = new Score();
	Image title;
	Graphics g;
	Repaint repaint;
	Judge judge;
	Motion motion;

	Window() {
		repaint();
		Score.scoreInit();
		time = new Time(this);
		this.setSize(509, 403);
		this.setLayout(null);
		/*
		b1=new JButton("Start");
		b1.setBounds(new Rectangle(150,180,200,30));
		b1.addActionListener(this);
		this.add(b1);
		*/
		this.addKeyListener(this);
		/*this.setLayout(new GridLayout(3, 1));
		b1 = new JButton("Start");
		b1.addActionListener(this);
		l1 = new Label("びじゅえるどブリッツ");
		l2 = new Label(count + "回目");
		this.add(l1);
		this.add(l2);
		this.add(b1);
		*/
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		count++;
	}
	public void paint(Graphics g){
		//sc.maxInit();
		this.g = g;
		title = getToolkit().getImage("img/title.jpg");
		g.drawImage(title, 0 , 20 , this);
		g.setFont(new Font("メイリオ", Font.ITALIC|Font.BOLD, 20));
		g.setColor(Color.BLACK);
		g.drawString("Highscore: " + sc.fileReader(), 275, 350);
	}

	public void core(){
		this.addMouseListener(this);
		//this.remove(b1);
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, 509, 403);
		g.dispose();
		/*this.remove(l1);
		this.remove(l2);*/
		this.setLayout(new BorderLayout());
		panel = new Map();				//SwapBlockからMap情報を読み出してたので、Mapの情報から貰うようにしました。
		this.add(panel, BorderLayout.CENTER);
		//new AddBlock().addBlock(panel.map);
		panel.repaint();
		judge = new Judge(panel);
		motion = new Motion(panel);
		//l3 = new Label("");
		//this.add(l3, BorderLayout.SOUTH);
		this.setSize(500, 550);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		time.startTime();
		new Thread(time).start();
		new Thread(motion).start();
		new Thread(judge).start();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	      	Point point = e.getPoint();
	      	ChangePoint p = new ChangePoint(); //changePointでSwapしたい行列の配列の行(swapX)と列(swapY)に変換するメソッド
	      	p.changePointX(point.x);
	      	p.changePointY(point.y);
	      	panel.swapBlock(p.swapX, p.swapY);

	      	//Destroy aoi = new Destroy(panel);
	      	//while(aoi.isExistance() == true){
	      		//panel.destroyBlock2(aoi);
	      	//	aoi = new Destroy(panel);

	      //	}
	}


	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO 自動生成されたメソッド・スタブ

	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ENTER){
			core();
		}
		if(e.isShiftDown()){
			time.fin();
			//repaint.fin();
			judge.fin();
			motion.fin();
			this.setVisible(false);
			new Window();
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO 自動生成されたメソッド・スタブ

	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO 自動生成されたメソッド・スタブ

	}
}
